package com.incms.web.modules.system.controller;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.incms.web.common.controller.BaseController;
import com.incms.web.common.util.Page;
import com.incms.web.modules.system.entity.SysMenu;
import com.incms.web.modules.system.entity.SysRole;
import com.incms.web.modules.system.service.SysMenuService;
import com.incms.web.modules.system.service.SysRoleService;

@Controller
@RequestMapping(value="/admin/sysrole")
@Slf4j
public class SysRoleController extends BaseController {
	
	@Resource(name = "sysRoleService")
	private SysRoleService sysRoleService;
	@Resource(name = "sysMenuService")
	private SysMenuService sysMenuService;
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Page<SysRole> page,SysRole sysRole,ModelMap modelMap){
		Page<SysRole> p = sysRoleService.queryRolePage(page, sysRole);
		modelMap.addAttribute("page", p);
		return "/admin/sys/role/role_list";
		
	}
	/**
	 *添加页面 
	 */
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(SysRole sysRole,ModelMap modelMap){
		List<SysMenu> menuList = sysMenuService.findAll();
		modelMap.addAttribute("allMenuList",menuList);
		return "/admin/sys/role/role_add";
	}
	
	/**
	 *添加操作 
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(SysRole sysRole,RedirectAttributes redirectAttributes,String menuIds){
		try {
			List<SysMenu> sysMenus = Lists.newArrayList();
			if(StringUtils.isNotBlank(menuIds)){
				sysMenus = sysMenuService.findIds(menuIds);
			}
			sysRole.setSysMenus(sysMenus);
			sysRoleService.save(sysRole);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.SUCCESS, "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, e.getMessage());
		}
		return "redircet:list";
	}
	
	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET )
	public String edit(Long sysRoleId,ModelMap modelMap){
		List<SysMenu> menuList = sysMenuService.findAll();
		modelMap.addAttribute("allMenuList",menuList);
		SysRole sysRole = sysRoleService.findRoleId(sysRoleId);
		List<SysMenu> sysMenus = sysRole.getSysMenus();
		StringBuffer checkMenuIds = new StringBuffer();
		for (int i = 0; i < sysMenus.size(); i++) {
			SysMenu m = sysMenus.get(i);
			if(i==0){
				checkMenuIds.append(m.getMenuId());
			}else{
				checkMenuIds.append(",").append(m.getMenuId());
			}
		}
		modelMap.addAttribute("sysRole",sysRole);
		modelMap.put("checkMenuIds", checkMenuIds.toString());
		return "/admin/sys/role/role_edit";
	}
	
	/**
	 * 更新操作
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String update(SysRole sysRole,RedirectAttributes redirectAttributes,String menuIds){
		try {
			sysRoleService.update(sysRole);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.SUCCESS, "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, e.getMessage());
		}
		return "redirect:list";
	}
	
	
	@RequestMapping(value = "/checkRoleName",produces="application/json;charset=utf-8;")
	@ResponseBody
	public Boolean checkRoleName(String roleName,String oldRoleName){
		if(roleName.equals(oldRoleName)) return false;
		return	sysRoleService.findCheckRoleName(roleName);
	}
}
